So, we have installed the latest Docker binary. In this recipe, we will start a new container with Docker. We will see some basic Docker commands to start and manage Docker containers.
Make sure that you have installed Docker and set your user as a member of the Docker group.
You may need sudo
privileges for some commands.
Let's create a new Docker container and start it. With Docker, you can quickly start a container with the docker run
command:
$ docker run -it --name dc1 ubuntu /bin/bash Unable to find image 'ubuntu:trusty' locally trusty: Pulling from library/ubuntu 6599cadaf950: Pull complete 23eda618d451: Pull complete ... Status: Downloaded newer image for ubuntu:trusty root@bd8c99397e52:/#
Once a container has been started, it will drop you in a new shell running inside it. From here, you can execute limited Ubuntu or general Linux commands, which will be executed inside the container.
exit
or pressing Ctrl + D. This will terminate your shell and stop the container as well.docker ps
command to list all the containers and check the status of your last container:$ docker ps -a
By default, docker ps
lists all running containers. As our container is no longer running, we need to use the -a
flag to list all available containers.
docker start
command. You can use the container name or ID to specify the container to be started:$ docker start -ia dc1
The -i
flag will start the container in interactive mode and the -a
flag will attach to a terminal inside the container. To start a container in detached mode, use the start
command without any flags. This will start the container in the background and return to the host shell:
$ docker start dc1
docker attach
:$ docker attach dc1
exit
or press Ctrl + C to exit the terminal and stop the container.docker inspect
command with the name or ID of the container:$ docker inspect dc1 | less
This command will list all the details of the container, including container status, network status and address, and container configuration files.
docker exec
. For example, the following command gets the environment variables from the dc1
container:$ docker exec dc1 env
This one gets the IP address of a container:
$ docker exec dc1 ifconfig
docker top
command:$ docker top dc1
docker stop
, which will gracefully stop the container after stopping processes running inside it:$ docker stop dc1
docker rm
to remove/delete it:$ docker rm dc1
We started our first Docker container with the docker run
command. With this command, we instructed the Docker daemon to start a new container with an image called Ubuntu, start an interactive session (-i
), and allocate a terminal (-t
). We also elected to name our container with the --name
flag and execute the /bin/bash
command inside a container once it started.
The Docker daemon will search for Ubuntu images in the local cache or download the image from Docker Hub if the specified image is not available in the local cache. Docker Hub is a central Docker image repository. It will take some time to download and extract all the layers of the images. Docker maintains container images in the form of multiple layers. These layers can be shared across multiple container images. For example, if you have Ubuntu running on a server and you need to download the Apache container based on Ubuntu, Docker will only download the additional layer for Apache as it already has Ubuntu in the local cache, which can be reused.
Docker provides various other commands to manage containers and images. We have already used a few of them in the previous example. You can get the full list of all available commands from the command prompt itself, by typing docker
followed by the
Enter key. All commands are listed with their basic descriptions. To get more details on any specific subcommand, use its help
menu, as follows:
$ docker rmi --help
Docker images can be used to quickly create runc
containers, as follows:
$ sudo apt-get install runc $ mkdir -p runc/rootfs && cd runc $ docker run --name alpine alpine sh $ docker export alpine > alpine.tar $ tar -xf alpine.tar -C rootfs $ runc spec $ sudo runc start alpine
$ man docker create