Docker Quick Commands
Command | Description |
---|---|
docker version | Provides details on your docker version |
docker run -it ubuntu bash | Downloads the image named 'ubuntu' from docker hub, if not on system and starts the bash program. Terminal will open the unbuntu images bash. Exit this by keying in 'exit' |
docker ps | Similar to 'ps' command on Unix and returns all the processes that are running containers |
docker ps -a | Displays all running and exited docker containers |
docker start id/container_name | Starts the container with the given Id or name. Even partial match of the id/name works |
docker stop id/container_name | Stops the given id(partial also works)/container_name |
docker rm id/container_name | Removes the given container from the system. You have to download again for the next time |
docker rm -f id/container_name | Force removes the given container from the system even if it is running. |
docker exec -it id/container_name /bin/bash | 'exec' executes the command that is given in the end. In this example the 'bash' |
docker logs id/container_name -f | Opens the log file with tail contents scrolling with fresh updates |
docker inspect id/container_name | Displays the configuration file of the docker |
docker rmi image_name | Remove the image with this command |
docker volume ls | Shows all the volumes that were created |
docker volume rm volume_name | Deletes the volume with the name volume_name |
docker volume create --name volume_name | Create a new volume with the given name |
docker network create network_name | Creates a network with the given name |
docker network rm network_name | Deletes network with the given name |
docker network ls | Displays all the networks created |
docker network prune | Delete all unused networks. Prune works with images/containers as well |
Flags that you can add in 'docker run' command
Flags | Description |
---|---|
-d | Detach the container after starting if it were opening a terminal then you are no longer on the container's terminal after start. |
--rm | removes the container after it is stopped |
-p host_port:container_port | Maps the host port number to the container port number |
--name | You can provide your own name for the container instead of it generating automatically |
-v host_path:container_path | Mount and map the given host path to the container path. E.g., of host path could be ${PWD} which represents the current working directory |
-w /your_directory | uses this working directory when the container opens the program |
--network network_name | Container is connected to the given network |
Creating an Image
Let us say you want to build an image by taking the existing image and adding your own program into it. Then you need to create a Dockerfile which contains step-by-step instructions on how to create such an image.
Here is an example. In this example, we want to create an image by taking the existing python image
print('hey there!')
And now the Dockerfile should contain the below content:
FROM python
RUN mkdir /myproject
COPY myprog.py /myproject/
WORKDIR /myproject
CMD python myprog.py
Build command
docker build -t mbcc .
The above command expects to find a Dockerfile in the current working directory where the command is run.
To list all the images
To see all the images present on your machine run the below command:
docker image ls
or
'docker images'
You will see that there is an image with REPOSITORY name being mbcc is created
Then you can run this docker run the below command
docker run mbcc
You will see 'hi there' printed on the console.
Reference: https://docs.docker.com/engine/reference/builder/
Re-tagging with your username
docker tag mbcc jravi123/python:latest
In the above command, jravi123 is your docker username and python:latest, represents, image_name/tag_name
Saving your image to Dockerhub
Login to docker hub from command prompt using
docker login
Then push your specific tagged version of the image to docker hub using the command below
docker push jravi123/python:latest
If the push fails, logout using
docker logout
and then login again.
Anyone can download this image using docker run jravi123/python:latest
Using Configuration YAML file
Instead of running the various commands from the command prompt, you can write a script that aggregates the various container services into one YAML file.
The file can be named as docker-compose.yml, docker-compose.yaml, compose.yml, compose.yaml
And you can run this script using docker-compose up
Here is an example of compose.yml file:
version: "3"
services:
jravi_python:
build:
context: ./
dockerfile: Dockerfile
Note - it needs a space after every colon!
While docker-compose up
uses an existing image, you can also build it using
docker-compose up build
Mounting volumes
You do not need to copy over the program files into your image and instead mount the filesystem by specifying that step in docker-compose file
Here are the instructions to add in your Dockerfile without the copy step
FROM python
RUN mkdir /myproject
WORKDIR /myproject
CMD python myprog.py
And the myprog.py is now in the host computer at the folder where docker-compose is run and the docker-compose file is as shown below
version: "3"
services:
jravi_python:
build:
context: ./
dockerfile: Dockerfile
volumes:
- "./:/myproject"
You will see an output 'hi there!' without any issues
Docker-compose commands
Command | Description |
---|---|
docker-compose ps | Displays all the running containers |
docker-compose down | Removes all the containers |
docker-compose run container_name | Will run the specific container |
docker-compose stop | Stops the containers |
docker-compose rm | Deletes the containers |