Introduction
In our last post, we explained how to install docker on a Centos 7 system. In this post, we will demonstrate how to launch and run our first container named hello-world. Before we launch our container let’s verify the version of Docker installed on the system and ensure that the docker container engine service is started.
[sahil@linuxnix ~]$ docker version Client: Version: 18.09.7 API version: 1.39 Go version: go1.10.8 Git commit: 2d0083d Built: Thu Jun 27 17:56:06 2019 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 18.09.7 API version: 1.39 (minimum version 1.12) Go version: go1.10.8 Git commit: 2d0083d Built: Thu Jun 27 17:26:28 2019 OS/Arch: linux/amd64 Experimental: false [sahil@linuxnix ~]$ [sahil@linuxnix ~]$ sudo systemctl enable docker Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service. [sahil@linuxnix ~]$ sudo systemctl start docker [sahil@linuxnix ~]$ [sahil@linuxnix ~]$ sudo systemctl status docker ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled) Active: active (running) since Sun 2019-07-07 16:15:42 UTC; 6s ago Docs: https://docs.docker.com Main PID: 8756 (dockerd) Tasks: 8 Memory: 32.1M CGroup: /system.slice/docker.service └─8756 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock Jul 07 16:15:42 linuxnix dockerd[8756]: time="2019-07-07T16:15:42.169081427Z" level=info msg="Successfully created filesystem xfs on device doc...icemapper Jul 07 16:15:42 linuxnix dockerd[8756]: time="2019-07-07T16:15:42.196379146Z" level=warning msg="[graphdriver] WARNING: the devicemapper storag... release" Jul 07 16:15:42 linuxnix dockerd[8756]: time="2019-07-07T16:15:42.235077254Z" level=info msg="Graph migration to content-addressability took 0.00 seconds" Jul 07 16:15:42 linuxnix dockerd[8756]: time="2019-07-07T16:15:42.236148156Z" level=info msg="Loading containers: start." Jul 07 16:15:42 linuxnix dockerd[8756]: time="2019-07-07T16:15:42.485560065Z" level=info msg="Default bridge (docker0) is assigned with an IP a... address" Jul 07 16:15:42 linuxnix dockerd[8756]: time="2019-07-07T16:15:42.639855438Z" level=info msg="Loading containers: done." Jul 07 16:15:42 linuxnix dockerd[8756]: time="2019-07-07T16:15:42.690116677Z" level=info msg="Docker daemon" commit=2d0083d graphdriver(s)=devi...n=18.09.7 Jul 07 16:15:42 linuxnix dockerd[8756]: time="2019-07-07T16:15:42.690414972Z" level=info msg="Daemon has completed initialization" Jul 07 16:15:42 linuxnix systemd[1]: Started Docker Application Container Engine. Jul 07 16:15:42 linuxnix dockerd[8756]: time="2019-07-07T16:15:42.735665770Z" level=info msg="API listen on /var/run/docker.sock" Hint: Some lines were ellipsized, use -l to show in full. [sahil@linuxnix ~]$
Now that we’ve verified that the Docker container engine is running on the system, let’s run our first container named hello-world. To run a docker container we use the docker run command followed by the container name which in this case is hello-world.
[sahil@linuxnix ~]$ sudo docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:41a65640635299bab090f783209c1e3a3f11934cf7756b09cb2f1e02147c6ed8 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/ [sahil@linuxnix ~]$
From the above output carefully consider the lines “Unable to find image ‘hello-world:latest’ locally” and “latest: Pulling from library/hello-world”. These two statements mean that the docker image required to launch the hello-world container was not locally available on the server. This image was downloaded from Docker hub and then the hello-world container was run from it. All this happened in the course of a single command invocation “docker run hello-world”. The rest of the output mainly serves the purpose to confirm that the docker container engine is working as expected on the system.
Note that when I used the docker run command, I added sudo to it. What if we run the command without sudo?
[sahil@linuxnix ~]$ docker run hello-world docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.39/containers/create: dial unix /var/run/docker.sock: connect: permission denied. See 'docker run --help'. [sahil@linuxnix ~]$
To allow non-root users to launch docker containers, they must be added in the docker group. This group gets created when you install Docker on your system.
[sahil@linuxnix ~]$ sudo usermod -a -G docker sahil
Now that we’ve added sahil to the docker group, let’s launch the hello-world container again and this time without the sudo.
[sahil@linuxnix ~]$ docker run hello-world Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/ [sahil@linuxnix ~]$
Notice that this time we do not see the image being pulled from Docker hub. This is because we already have a local copy of the hello-world container image available on the system. Docker downloaded this copy onto the system when we executed the docker run command earlier.
Conclusion
This concludes our post showing you how to launch your first docker container. We hope that you found this post to be useful and would appreciate your suggestions and feedback.
Sahil Suri
Latest posts by Sahil Suri (see all)
- Google Cloud basics: Activate Cloud Shell - May 19, 2021
- Create persistent swap partition on Azure Linux VM - May 18, 2021
- DNF, YUM and RPM package manager comparison - May 17, 2021
- Introduction to the aptitude package manager for Ubuntu - March 26, 2021
- zypper package management tool examples for managing packages on SUSE Linux - March 26, 2021