Create Your First Container in Docker
There are a number of commands which are required to manage Docker containers and images. Let’s see if we have any images in our local Docker library.
learning-ocean:~ gaurav$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE Gauravs-learning-ocean:~ gaurav$
The docker images command lists the available local images which you can use to create a Docker container. The above output does not show any local images.
Let’s use the search command to find an image to download. using docker search ubuntu command
learning-ocean:~ gaurav$ docker search ubuntu NAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Debian-based Linux operating sys 12455 [OK] dorowu/ubuntu-desktop-lxde-vnc. Docker image to provide HTML5 VNC interface …. 544 [OK]
It will first search if we have any images locally. If not, it will search it on the docker hub registry. This will display a list of all the images available containing the word ubuntu.
Let's download the basic ubuntu 14.04 image:
learning-ocean:~ gaurav$ docker image pull ubuntu:14.04 14.04: Pulling from library/ubuntu 2e6e20c8e2e6: Download complete 0551a797c01d: Download complete 512123a864da: Download complete Digest: sha256:5c01e896fa6eeaa41f3509c64af668d71d06e318cfe373dabab9d61b9eaf6441 Status: Downloaded newer image for ubuntu:14.04 docker.io/library/ubuntu:14.04
You can check this has downloaded the image,
learning-ocean:~ gaurav$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 14.04 13b66b487594 3 months ago 197MB learning-ocean:~ gaurav$
Create the Docker container with the run command and specify the bash shell to be executed in the container.
$ docker container run ubuntu:14.04 cat /etc/os-release
learning-ocean:~ gaurav$ docker container run ubuntu:14.04 cat /etc/os-release NAME="Ubuntu" VERSION="14.04.6 LTS, Trusty Tahr" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 14.04.6 LTS" VERSION_ID="14.04" HOME_URL="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/" BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/" learning-ocean:~ gaurav$
As we run this command, we will come out from the container as soon as we run it and the container will be stopped.
Run the docker container ls Docker command to see what running containers are there now:
learning-ocean:~ gaurav$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES learning-ocean:~ gaurav$
Now run the below command to see all containers (running container as well as stopped containers):
learning-ocean:~ gaurav$ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f087609faa23 ubuntu:14.04 "cat /etc/os-release" 5 minutes ago Exited (0) 5 minutes ago relaxed_kilby learning-ocean:~ gaurav$
To remove a container, simple use rm command provided by docker as -
learning-ocean:~ gaurav$ docker container rm f087609faa23
Here, you must provide the id or name of the container. The id must be unique. So, you can provide the id as a single character also as long as it is unique. You can give multiple container ids or name at a time to remove more than 1 container at a time.
learning-ocean:~ gaurav$ docker container rm relaxed_kilby relaxed_kilby learning-ocean:~ gaurav$