1. Home
  2. Training Library
  3. Containers
  4. Courses
  5. Getting started with Docker

Logging into a Docker Container

Contents

keyboard_tab
Introduction
1
What is Docker?
PREVIEW2m 42s
Start course
Overview
Difficulty
Beginner
Duration
42m
Students
2974
Description

It's hardly an exaggeration to say that there hasn't been an innovation in app development and deployment over the past year or two that's been bigger than Docker. The open source platform for building, shipping, and running distributed applications in isolated virtual containers has literally changed the very fabric of the cloud. Thanks to Docker, software developers can reliably create and deploy applications with all their dependencies in Docker images that can be made available to anybody, and launched in seconds.

This introductory course, crafted by the expert Linux System Administrator David Clinton, will teach you everything you need to know to get started with Docker: What is the LXC software that it's built upon? How is it different from traditional virtualization technologies? How you can install and run it on your machine? By the end of this course, you'll have all the basic tools you'll need to run simple Docker applications.

Who should take this course

Being an introductory course, no previous knowledge of the software is needed. Nevertheless, you will definitely need at least some experience with the Linux Command Line Interface. Also, some familiarity with the Linux file system, kernel and networking basics might came in handy for some lectures.

Do you have questions on this course? Contact our cloud experts in our community forum.

Transcript

Hi and welcome to CloudAcademy.com's video series on Docker, the development oriented virtualization service. In this video, we're going to discuss entering a Docker container.

How to log into a Docker container

Until now, we've seen how to run commands from outside a Docker container, and we've seen some output from those commands. But how do we actually get into the Docker container as though we were actually running a regular computer with a regular shell session?

There are two ways. The first is using the command run, and associating it specifically with a shell session. "sudo docker run -i -t ubuntu /bin/bash". That is, we will enter an interactive session of the image, ubuntu using bin bash or the bash shell.

And we're in. As we've seen before, this is what seemed to be pretty much a full interface, as though you were in a regular computer. Hit ifconfig as an example, and we see we've got a proper IP address. Ping gmail.com, let's say, and we seem to have ping access.

There are two major drawbacks with entering a Docker container this way. The first is that when we type exit and leave, the container will die with it. The second is that there are certain things we can't do. Let's say we wanted to mount a volume, mount-t for type, temp fs, let's say. We'll call it stuff and we'll mount it to mnt. Permission denied. We can't do it. We can't run any command as though we are root. Let's exit, and let's confirm that the session has actually died. "sudo docker ds", and there's no running session.

Log into a Docker container as root

Let's run this a different way: let's "sudo docker run", but instead of just going straight to the Docker container type and name, let's first add privileged. That is, let's run this session with the equivalent of sudo rights and see what happens. We're in.

Let's try mounting that volume we wanted to mount before. So, mount -t, temp fs, stuff to the mnt directory. Seems we got away with it. Let's run DF, that is a list of all of our mounted volumes. It's there. It's a little hard to read, I'm afraid. But towards the bottom, there is the line stuff, and in fact it is mounted to the mnt directory.

We now know how to enter a Docker container as root. Let's exit and that container has died along with our exit. We dealt with the proble, of root privileges. But we haven't overcome the issue of leaving, exiting a session while somehow allowing it to live on.

Start a Docker session with nsenter

This, we can accomplish using nsenter. Technically, we could also do it by SSHing in. However, that is not recommended by a lot of the people who have loads of experience using Docker. Let's focus on nsenter. The nsenter package that comes in the current Ubuntu 14.04 repositories is a few versions too old, apparently. So you're going to have to download and compile nsenter manually, using at least version 2.23. At this point, I believe version 2.24 exists. We have nsenter installed on this system already, so we'll just show how we use it.

First thing we want to do is to get the container ID of an existing Docker container. Do that with "sudo docker ps". We've discovered that there is a Docker container not running currently with the ID BC897B1709D4. First, "sudo docker start" to get it running. Let's make sure it actually is running. "sudo docker ps", without the -A, so we're only going to display those containers that are actually running. And in fact, this one is now running. It's been up a grand total of 11 seconds. The next thing we're going to do is find the PID, the process ID of this Docker container. We'll do this with the "sudo docker inspect" command, specifically looking for the process ID, the PID, which is 1954. We'll now enter the, we'll use sudo nsenter to act on this process.

Let's enter that process ID, 1954. And it seems that we are in. We seem to be in the session, just the way we'd like to be.

Let's exit and see if the session has survived our exit. We'll do that with the "sudo Docker ps" and again, not -A, so we're only going to return those sessions that are still alive. And the container is, in fact, still alive.

About the Author
Avatar
David Clinton
Linux SysAdmin
Students
10816
Courses
12
Learning Paths
4

David taught high school for twenty years, worked as a Linux system administrator for five years, and has been writing since he could hold a crayon between his fingers. His childhood bedroom wall has since been repainted.

Having worked directly with all kinds of technology, David derives great pleasure from completing projects that draw on as many tools from his toolkit as possible.

Besides being a Linux system administrator with a strong focus on virtualization and security tools, David writes technical documentation and user guides, and creates technology training videos.

His favorite technology tool is the one that should be just about ready for release tomorrow. Or Thursday.

Covered Topics