Docker is a popular containerization platform that simplifies developer’s life by enabling rapid setup of local environments. In this post, weโll explore some essential Docker commands that every developer should know. We will also learn some of the common instructions used in the docker file .
Table of Contents
Docker commands
| Docker Commands | |
| docker system prune docker <resource> prune | This command is used to remove all the stopped containers, dangling images, unused network, unused build cache, etc. We can also prune the individual docker resources like image, volume, etc. |
| docker images | Lists all the images stored in the local system. |
| docker run <image-name> | Runs a docker container from the specified image. Pull the image, if it is not available locally. Ex: docker run nginx |
| docker ps docker ps -a | Lists all the running containers. With the -a flag, existed containers are also listed. |
| docker run <image-name> <command> | Run a docker container and execute the command. Ex: docker run ubuntu date (starts an ubuntu container, prints the date and exits the container) |
| docker run -it <image-name> | Runs the container in interactive mode. Ex: docker run -it ubuntu (Runs the ubuntu container and also gives a interactive shell to execute the command) |
| docker run –name=<name> <image-name> | Assigns the container a name. Ex: docker run –name=my-db postgres |
| docker start/stop <container-id/name> | To start or stop an existing docker container. Ex: docker stop my-db |
| docker exec <container-name> <command> | This commands helps in executing a command in a running container. |
| docker run -p <host-port>:<conatiner-port> <image-name> | Run a docker container with exposing the port to the host. Ex: docker run -p 5432:5432 postgres |
| docker run -d <image-name> | Run the container in the background(detached mode) |
| docker logs <container-id/name> | Access docker container logs. |
| docker run -v /host-path:/container-path <image> | Maps the host directory as container volume. Ex: docker run -v /abc:/data/static/html nginx |
| docker network create <name> | Creates a new Bridge network. |
| docker run -n <network-name> <image-name> | Runs a docker container with custom network. |
| docker network ls | Lists all the available docker networks. |
| docker build <Dockerfile-path> docker build -f <myDockerfile-path> | Builds the docker image locally from the specified Dockerfile path. Also, The name of the docker file (other than Dockerfile) can be specified with the -f flag. |
Docker image
A simple docker image may contain below instuctions.
| FROM <base-image> | Every Dockerfile needs a base image. Ex: FROM ubuntu |
| ADD <source> <destination> COPY <source> <destination> | Adds files from host to container. ADD command supports HTTP URLs as well. Ex: COPY /a/init.sql /data/init.sql |
| RUN <command> | Run command during image build process. EX: RUN mkdir /test |
| ENV <key>=<value> | Defines environment variables, that gets passed to the container. These can be overridden using the -e flag while running the container. Ex: ENV KEY=value |
| WORKDIR <path> | Sets the container working directory. |
| EXPOSE <port> | Exposes the mentioned port of the container. Ex: EXPOSE 80 |
| CMD <command> | Default command to execute after the container starts. IT can be overridden while running the container. This can be in the shell form or in the exec(array of commands as shown below) form. Ex: CMD [“java”, “-jar”, “app.jar”] |
| ENTRYPOINT <command> | Defines the main executable command. |
Conclusion
In this article, we saw some of the basic docker commands.
We also learned commonly used and useful instructions used in the Docker file.
Basic Docker Commands Every Developer Should Know
Discover more from ASB Notebook
Subscribe to get the latest posts sent to your email.