Docker commands

Once Docker is installed on the host machine, it runs as a daemon process. The interface given to users is a Docker client. Communication between Docker daemon and users happens through Docker client. Docker provides a variety of commands for different needs, which helps to automate the deployment process very easily. Now we will learn different Docker commands. As this is not a Docker book, the discussion will be limited to some basic commands. You can refer to the following Docker website for the complete reference guide at https://docs.docker.com/reference/.

Help command

Once Docker is installed, to see the list of all the commands supported you can type docker help.

This command lists all the available Docker commands. The basic syntax of a Docker command is docker <options> command <argument>.

Download image

As we mentioned earlier, Docker provides its own public repository from where you can download the images to get started with Docker. You do not need to reinvent the wheel by creating the image, unless needed. In the repository, you can find lots of images varying from plain vanilla OS to images embedded with Java, Tomcat, MySQL, and so on. To download an image from the repository, you can use the docker pull <image name> command, as follows:

$ docker pull ubuntu

latest: Pulling from ubuntu

e9e06b06e14c: Pull complete 
a82efea989f9: Pull complete 
37bea4ee0c81: Pull complete 
...

By default, this command pulls images from the public Docker registry, but you can configure private registry as well.

The list of images

Once the image is downloaded, you can find the list of images using the docker images command, as follows:

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED VIRTUAL SIZE
<none>              <none>              07f8e8c5e660        14 hours ago        188.3 MB
python              2.7                 912046e33f03        8 days ago          747.9 MB
ubuntu              latest              d0955f21bf24        6 weeks ago         188.3 MB

It will list all the downloaded images available in the filesystem. You can create one or many containers using images.

Creating a container

Once image is downloaded, you can create a container using the Docker run command, as follows:

$ docker run -dit --name "testUbuntu1" ubuntu /bin/bash
b25a9d5806a71f411631c4bb5c4c2dd4d059d874a24fee2210110ac9e8c2909a

This command creates a container named testUbuntu1 from the image Ubuntu and the command we have mentioned is /bin/bash, to just execute a shell or command-line interface. The output of this command is the container ID. You can access the container by its name tesUbuntu1 or by the container ID.

Here the -d option will start it as a daemon process, the -i option is for interactive, and the -t option is to allocate a pseudo-TTY. Let's create another container as follows:

$ docker run -dit --name "testUbuntu2" ubuntu /bin/bash
f9cdd046cbf47f957ef972690592245f27784f5f79ded6ca836afab54b4f9a8f

It will create another container with the name testUbuntu2. You can create many containers with the same image by giving different names. If you do not specify any name, Docker assigns some default name. The syntax of the run command is $ docker run <options> <imagename> <command>.

The container list

To find the list of running containers use the Docker ps command, as shown here:

$ docker ps 
CONTAINER ID      IMAGE             COMMAND         CREATED         STATUS            PORTS             NAMES
b25a9d5806a7      ubuntu:latest     /bin/bash         2 minutes ago   Up 2 minutes                          testUbuntu1 

Here, we have created two containers but the output shows only one container testUbuntu1 that is running. Run the same command now with the –a option, as follows:

$ docker ps -a
CONTAINER ID      IMAGE             COMMAND           CREATED         STATUS                   PORTS             NAMES
f8148e333eb3      ubuntu:latest     echo hello world  7 seconds ago   Exited (1) 7 seconds ago                     testUbuntu2       
b25a9d5806a7      ubuntu:latest     /bin/bash         3 minutes ago   Up 3 minutes                                 testUbuntu1

The output lists all the containers with individual status. Notice that the testUbuntu2 container is exited, that is, stopped, whereas testUbuntu1 is still running.

Start/stop container

Once the container is created from the image, it can be started/stopped using the following commands:

$ docker start|stop containername|containerid

Following is an example of the preceding command:

$ docker stop testUbuntu1
$ docker start f8148e333eb3

Connecting to a container

If you have started a container and then you want to connect to the running container console, the Docker attach command can be used as shown here:

$ docker attach testUbuntu1
[Enter]
root@b25a9d5806a7:/#

Use Ctrl + P + Q to exit the container. The exit or ^C command will take you out of the container and additionally, it will stop the running container by killing all the running processes. If you want to just move out of the container without stopping, use Ctrl + P + Q. These commands could be different based on operating systems. Refer to the Docker documentation for more details.

Deleting a container

The Docker rm command deletes or removes a container from the machine, as follows:

$ docker rm testUbuntu2
testUbuntu2

You can check whether it is deleted properly by running the docker ps -a command:

Removing an image

To remove an image from the system, use the docker rmi command. This command will remove the image from the machine. You need to stop any running container before removing the image. This is done as follows:

$ docker rmi ubuntu

Copying files to the container

With the UNIX cp command, a file can be copied from the host to the container. For example, the following command copies the dir1 folder from the host system to the container's /home/mycontents directory. Here we have to provide the absolute path of the container installed in the host machine:

$sudo cp -r dir1 /var/lib/docker/aufs/mnt/b25a9d5806a71f411631c4bb5c4c2dd4d059d874a24fee2210110ac9e8c2909a/home/mycontents/

But this is not a good practice. Alternative solution is to mount the directories when creating the container with the –v option:

$ docker run -ditP --name testUbuntu -v /home/user1/dir1:/home/dir1 ubuntu

The preceding command will create a container named testUbuntu. The command also maps the /home/user1/dir1 directory of host machine to the /home/dir1 directory of the container.

To copy the contents from the container to host machine, the docker cp command can be used, as shown here:

$ docker cp testUbuntu1:/home/dir1/readme.txt .

Container details

The Docker inspect command helps to find the complete details of container run as follows:

$ docker inspect testUbuntu1
[{
  "Args": [],
  "Config": {
    "AttachStderr": false,
    "AttachStdin": false,
    "AttachStdout": false,
    "Cmd": [
    "/bin/bash"
    ],
    "CpuShares": 0,
    "Cpuset": "",
    "Domainname": "",
    "Entrypoint": null,
    "Env": [
    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
    ],
...
...
    "PublishAllPorts": false,
    "VolumesFrom": null
  },
  "HostnamePath": "/var/lib/docker/containers/b25a9d5806a71f411631c4bb5c4c2dd4d059d874a24fee2210110ac9e8c2909a/hostname",
  "HostsPath": "/var/lib/docker/containers/b25a9d5806a71f411631c4bb5c4c2dd4d059d874a24fee2210110ac9e8c2909a/hosts",
"Id": "b25a9d5806a71f411631c4bb5c4c2dd4d059d874a24fee2210110ac9e8c2909a",
"Image": "d0955f21bf24f5bfffd32d2d0bb669d0564701c271bc3dfc64cfc5adfdec2d07",
  "MountLabel": "",
  "Name": "/testUbuntu1",
  "NetworkSettings": {
      "Bridge": "docker0",
      "Gateway": "172.17.42.1",
      "IPAddress": "172.17.0.22",
      "IPPrefixLen": 16,
      "PortMapping": null,
      "Ports": {}
  },
  "Path": "/bin/bash",
...
...
}

It will provide the complete detail of the container such as name, path, network settings, IP address, and so on.

Updating DNS settings

To update DNS settings, you can edit the /etc/default/docker file. You can change proxy setting and DNS setting in this file. The content of the file is shown as follows:

# Docker Upstart and SysVinit configuration file

# Customize location of Docker binary (especially for development testing).
#DOCKER="/usr/local/bin/docker"

# Use DOCKER_OPTS to modify the daemon startup options.
#DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"

# If you need Docker to use an HTTP proxy, it can also be specified here.
#export http_proxy="http://127.0.0.1:3128/"

# This is also a handy place to tweak where Docker's temporary files go.
#export TMPDIR="/mnt/bigdrive/docker-tmp"

Networking in an important concept and you should spend more time reading about it. More details can be found at https://docs.docker.com/articles/networking/.

Creating an image from a container

You might be interested in creating new images from the base container with additional software. Consider an example where you have created the testUbuntu1 container from the base Ubuntu image. Then you have installed Tomcat server, deployed web application, and maybe you have installed some other required software like Ant, Git, and so on. You might want to save all the changes for future. The following docker commit command is useful in this scenario:

$ docker commit  -m "Creating new image" testUbuntu1 user1/ubuntu_1

This command will create a new image user1/ubuntu_1 that will comprise a basic Ubuntu image and all the applications installed by you on that container. This command will commit the new image to the local repository. Next time, you can start a container from the new image.

$ docker run -dit --name testUbuntu_1  user1/ubuntu_1

This command will create the testUbuntu_1 container using the new image committed earlier. If you have created an account in the Docker repository (https://registry.hub.docker.com), you can even push the new images to the public repository.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.45.5