Docker
Docker CLI
Basic Commands
docker --version
Check Docker version.
docker --version
docker info
Display system-wide information.
docker info
docker help
Get help on Docker commands.
docker help
Images
docker images
List all Docker images.
docker images
docker pull [image]
Pull an image from a registry.
docker pull ubuntu:latest
docker rmi [image_id]
Remove a Docker image.
docker rmi ubuntu:latest
Containers
docker ps
List running containers.
docker ps
docker ps -a
List all containers (running and stopped).
docker ps -a
docker run [options] [image]
Run a command in a new container.
docker run -it ubuntu:latest /bin/bash
docker stop [container_id]
Stop a running container.
docker stop [container_id]
docker start [container_id]
Start a stopped container.
docker start [container_id]
docker restart [container_id]
Restart a container.
docker restart [container_id]
docker rm [container_id]
Remove a stopped container.
docker rm [container_id]
Networks
docker network ls
List all networks.
docker network ls
docker network create [network_name]
Create a new network.
docker network create my_network
docker network rm [network_name]
Remove a network.
docker network rm my_network
Volumes
docker volume ls
List all volumes.
docker volume ls
docker volume create [volume_name]
Create a new volume.
docker volume create my_volume
docker volume rm [volume_name]
Remove a volume.
docker volume rm my_volume
Docker Compose
docker-compose up
Create and start containers.
docker-compose up
docker-compose down
Stop and remove containers, networks, images, and volumes.
docker-compose down
docker-compose build
Build or rebuild services.
docker-compose build
Inspect and Logs
docker inspect [container_id]
Return low-level information on Docker objects.
docker inspect [container_id]
docker logs [container_id]
Fetch the logs of a container.
docker logs [container_id]
Clean Up
docker system prune
Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.
docker system prune
docker container prune
Remove all stopped containers.
docker container prune
docker volume prune
Remove all unused volumes.
docker volume prune
docker image prune
Remove unused images.
docker image prune
This cheat sheet covers the basic and most commonly used Docker commands to get you started. Feel free to ask if you need more details or advanced commands!
Creating a Dockerfile
A Dockerfile is a text file that contains a series of instructions on how to build a Docker image. Here's a basic overview and examples of common Dockerfile instructions:
Basic Structure of a Dockerfile
FROM
Specifies the base image.
FROM ubuntu:latest
RUN
Executes commands in a new layer on top of the current image.
RUN apt-get update && apt-get install -y nginx
COPY
Copies files from the host machine to the Docker image.
COPY ./localfile /path/in/container
ADD
Copies files/directories from the host machine to the Docker image, and also supports extracting tar files.
ADD ./localfile.tar /path/in/container
CMD
Specifies the command to run within the container.
CMD ["nginx", "-g", "daemon off;"]
ENTRYPOINT
Sets a default application to be used every time a container is created with the image.
ENTRYPOINT ["nginx", "-g", "daemon off;"]
EXPOSE
Informs Docker that the container listens on the specified network ports at runtime.
EXPOSE 80
ENV
Sets environment variables.
ENV ENVIRONMENT production
VOLUME
Creates a mount point with the specified path and marks it as holding externally mounted volumes from the native host or other containers.
VOLUME /data
Example Dockerfile
Here's an example Dockerfile for a simple web server using Nginx:
# Use the official Nginx image from the Docker Hub
FROM nginx:latest
# Set the maintainer label
MAINTAINER Your Name <[email protected]>
# Copy custom configuration file from the host to the container
COPY nginx.conf /etc/nginx/nginx.conf
# Copy the content of the website to the container
COPY ./html /usr/share/nginx/html
# Expose port 80 to the host
EXPOSE 80
# Start Nginx when the container launches
CMD ["nginx", "-g", "daemon off;"]
Building and Running the Docker Image
Build the Docker Image
Use the
docker build
command to create an image from the Dockerfile.
docker build -t my-nginx-image .
Run the Docker Container
Use the
docker run
command to start a container from the image.
docker run -d -p 80:80 my-nginx-image
Best Practices
Keep Dockerfile Instructions Ordered: Use a logical order such as
FROM
,MAINTAINER
,RUN
,COPY
,CMD
.Use .dockerignore: Create a
.dockerignore
file to exclude files and directories from the build context to reduce the size of the image.Minimize Layers: Combine multiple
RUN
commands to reduce the number of layers.Leverage Caching: Order the instructions to leverage Docker’s build cache.
This should give you a good starting point for creating your own Dockerfiles! If you have any specific questions or need further details, feel free to ask.
Last updated
Was this helpful?