Skip to main content

Docker Operations

This guide provides step-by-step commands for Docker installation and basic container operations.


Docker Installation

CommandDescription
apt updateUpdates the package sources with the latest versions
apt install docker.io -yInstalls Docker in Ubuntu Linux distribution
systemctl start dockerStarts Docker services
systemctl stop dockerStops Docker services
systemctl restart dockerStops and starts Docker services
systemctl status dockerChecks the status of Docker services

Container Operations

CommandDescription
docker container create --name web -it alpine shCreates a named container
docker container start webStarts a container
docker container stop webStops a container
docker container restart webStops and starts a container
docker container run -it --name myweb alpine shCreates and starts a named container
Ctrl + p, Ctrl + qDetaches from the container
docker container attach mywebAttaches to a running container
docker container logs mywebShows the logs of the container
docker container rename myweb myalpineChanges the name of a container
docker container run -h alpine -it --rm alpine shSets the hostname of a container
docker container run -it -w /var --rm alpine shSets the current working directory
docker container run -it --env "WEB_HOST=172.168.1.1" --rm alpine shSets the environment variables

Docker Management

CommandDescription
docker versionPrints the current Docker version
docker infoDisplays system-wide information
docker loginLogs in to a DockerHub registry
docker logoutLogs out from DockerHub registry
docker inspect [object]Displays detailed information on one or more objects
docker --helpLists help for any Docker command
docker image [option]Manages images
docker container [option]Manages containers
docker volume [option]Manages volumes
docker network [option]Manages networks

Listing Commands

CommandDescription
docker image lsLists all images
docker image ls --digestsLists image digests
docker container lsLists running containers
docker container ls -aLists all containers (running and stopped)
docker container ls -qLists only the container IDs
docker volume lsLists all volumes
docker network lsLists all networks

Docker Images

CommandDescription
docker search nginxList available images on DockerHub
docker image pull nginxPull an image from DockerHub
docker image pull myregistry.com:8080/alpine:latestPull an image from a private registry
vim DockerfileEdit Dockerfile
FROM alpine RUN date > /dataDockerfile to build an image
docker image build -t php/alpine:dockerfile .Create an image from a Dockerfile
docker container run -it --name myimage alpine shRun a container and modify it
/ # date > /data and / # cat /dataSave data to a file and display it
docker container diff myimageCheck differences with the base image
docker container commit myimage php/alpine:testSave a running container as an image
docker container run -it php/alpine:test sh and cat /dataRun the created image as a container
docker image push php/alpine:testPush the image to DockerHub registry

Docker Volumes

CommandDescription
docker container run -itv /data --name myvol alpine shCreate and mount a directory on the host system
docker volume create --name myvolumeCreate a named volume
docker container run -itv myvolume:/data --name cmyvol alpine shMount a named volume
docker system dfDisplay information about disk storage

Docker Networking

CommandDescription
docker container run -d --name myweb -p 8080:80 nginxAccess the container from the external world
docker container run -d --name myweb1 -P nginx:alpineExpose a container port to a random host port
docker container run -it --network=none alpine shPrevent Docker daemon from creating a network interface
docker container run -it --network=host alpine shShare host's network namespace with the container
docker container run -it --network=container:myweb1 alpine shShare network namespace of an existing container
docker network create mynetworkCreate a user-defined network
docker container run -d --name nginxnet --network=mynetwork nginx:alpineCreate a container and attach it to the network
docker network connect mynetwork webAttach a running container to a network
docker network disconnect mynetwork webDisconnect a container from a specific network

Docker Resource Allocation

CommandDescription
docker container run -it --ulimit nproc=10 --rm alpine shSet the maximum number of processes for the container
docker container run -d --name cpulimit --cpuset-cpus="0" alpine topRestrict the container to CPU "0"
docker container run -d --name memlimit --memory "200m" alpine topLimit the maximum memory usage