Useful Docker Commands for Beginners
Docker has become a universal software delivery tool, regardless of its structure, dependencies, or installation method. It’s important for a developer to have some knowledge of what docker is. As well as they have to know some important docker commands. In this article, I’ll try to point out the docker commands a beginner must know.
As I mentioned earlier docker has become a universal software delivery tool, regardless of its structure, dependencies, or installation method. In other words, Docker removed the dependency hell problem and made the infrastructure immutable. Being a developer or a CS student it’s good to have some knowledge about docker. You will get a basic overview of docker in my previous article(here). Now let get started. First, we have to install it on our machine.
Installation
If you are using a Linux machine(Ubuntu) you can install and use docker via terminal. To install git you have to run the following commands on your terminal.
Step-1: Updating Repository
It’s a good idea to update the local database of your software. To do so, run the following command in your terminal.
$ sudo apt-get update
Step-2: Remove prior installations
You need to make sure that your system does not have any prior Docker software installation that may be outdated. For that run the following command.
$ sudo apt-get remove docker docker-engine docker.io
Step-3: Install Docker
$ sudo apt install docker.io
Step-4: Check Version
To verify whether the installation has been successful or not and to check the docker version you have just installed run the following command.
$ docker --version
If docker is installed successfully you will see an output like this.
Step-5: Start and Automate Docker
If you want, Docker to be up and running at system startup, this step is for you. To start the Docker engine run,
$ sudo systemctl start docker or
$ sudo service docker start
For starting the docker on system startup, run
$ sudo systemctl enable docker
Congratulations! You have just installed docker successfully. Let’s go ahead.
Display Docker version and information
For docker version,
$ docker --version
Docker info
To see the information(summary of images/containers etc.) about docker on your machine run the following command
$ sudo docker info
Get rid of using sudo everytime
You may get the following error while running a docker command.
The error message tells you that your current user can’t access the docker engine, because you’re lacking permissions to access the UNIX socket to communicate with the engine.
As a temporary solution, you can use sudo
to run the failed COMMAND as root ( e.g -$ sudo docker info
). But if you want “sudo” not to be required every time for running docker commands, do the following.
Step-1:
$ sudo groupadd docker
Step-2:
$ sudo usermod -aG docker $USER
List Docker CLI commands
Run the following command in your terminal to see the list of docker commands.
$ docker
Run docker COMMAND --help
for more information on a specific command. For example,
$ docker image --help
Docker Image Commands
Show List of Docker Images
To see the list of docker images run the following command
$ sudo docker image ls
If you want to see all the images then run the next command
$ sudo docker ls -a
Run Image
$ sudo docker run image-name
If your machine doesn’t have the image then it willbe pulled from docker hub.
Remove Docker Image
To remove a specific docker image from your machine you have to run the following command.
$ sudo docker image rm <IMAGE_ID> or
$ sudo docker rmi <IMAGE_ID>
You will be able to get the image id for a specific docker image from $ sudo docker image ls
command. Sometimes, you may have to add --force
option in the command to force remove an image.
sudo docker rmi --force <IMAGE_ID>
If you want to delete all of the images from your machine at a time then run the following command.
$ sudo docker image rm $(docker image ls -a -q)
Build a Docker Image and run Container
If you want to build a docker image, you have to write a Dockerfile in your project folder first. Suppose, you want to build a weather web app using Vue.js your Dockerfile will be something like as below.
You will find more information about Dockerfile here.
After that go to your project folder and run the following command to build an image.
$ sudo docker build -t <image-name>:<version> .
Replace “<image-name>” and “<version>” according to your need.
Note: version is optional here.
Run container and map container port 8000 to host port 5000
$ sudo docker run -p 5000:8000 <image-name>
If you want to run the container in detached mode then run the next command
$ sudo docker run -d -p 5000:8000 <image-name>
In case time is not synced with the host machine,
$ sudo docker run -v /etc/localtime:/etc/localtime:ro -p 5000:8000 <image-name>
Docker Container Commands
Container List
To see a list of all of the containers in your machine run
$ sudo docker container ls -a
To see the running containers,
$ sudo docker container ls
For all containers in quiet mode,
$ sudo docker container ls -aq
For existing docker containers in running state that was not listed in ls command run the following command.
$ sudo docker container ps or
$ sudo docker ps
Stopping a Container
To stop a container gracefully,
$ sudo docker container stop <CONTAINER_ID>
To shutdown a container forcefully,
$ sudo docker container kill <CONTAINER_ID> or
$ sudo docker container stop --force <CONTAINER_ID>
Remove Docker Container
To remove a specific docker Container from your machine you have to run the following command.
$ sudo docker container rm <CONTAINER_ID>
Sometimes, you may have to add --force
option in command to force remove a container.
If you want to delete all of the containers from your machine at a time then run the following command.
$ sudo docker container rm $(docker container ls -a -q)
Container Logs
To see specific container log (stdout/stderr)
$ sudo docker logs -f <CONTAINER_ID>
Go inside a container
Go inside a running container and run a command,
$ sudo docker exec -it <CONTAINER_ID> bash
Thanks for reading. In my next article, I’ll discuss docker remote and docker hub.