Docker common commands

https://docker-curriculum.com/

https://docs.docker.com/get-started/

 

  • Install
apt-get update
apt-get install docker.io sudo usermod -a -G docker $USER 
systemctl start docker
systemctl enable docker

then make a logoff , use groups to check you are in docker group or not:

groups

 

  • build image:

create a Dockerfile file and then run

docker build -t your_image .

list images and containers

docker images docker images-l
docker container ls --all

run a container and check the port:

docker run -d -p 8800:80 --name your_container your_image
docker port your_container

detached run the image as a container and map the 8800 port of the container to host 80 port

Then we can check that the new container with name  based on ‘nginx_image’ is running, List the running containers:

docker ps

Or check all containers

docker ps -a
docker stop your_container
docker rm your_container
docker rmi your_image
Pull an image from a registry
docker pull alpine:3.4
Retag a local image with a new image name and tag
docker tag alpine:3.4 myrepo/myalpine:3.4
Log in to a registry (the Docker Hub by default)
docker login my.registry.com:8000
Push an image to a registry
docker push myrepo/myalpine:3.4
Stop a running container through SIGTERM
docker stop my_container
Stop a running container through SIGKILL
docker kill my_container
Create an overlay network and specify a subnet
docker network create --subnet 10.1.0.0/24 --gateway 10.1.0.1 -d overlay mynet
List the networks
docker network ls
Delete all running and stopped containers
docker container prune Or this command in old versions: docker rm -f $(docker ps -aq)
 docker rm 305297d7a235 ff0a5c3750b9
Create a new bash process inside the container and connect it to the terminal
docker exec -it my_container bash
Print the last 100 lines of a container’s logs
docker logs --tail 100 my_container
  • docker run – Runs a command in a new container.
    docker run hello-world
    docker run -it busybox sh

     

  • docker start – Starts one or more stopped containers
  • docker stop – Stops one or more running containers
  • docker build – Builds an image form a Docker file
  • docker pull – Pulls an image or a repository from a registry
docker pull busybox
docker pull ubuntu:16.04
  • docker push – Pushes an image or a repository to a registry
  • docker export – Exports a container’s filesystem as a tar archive
  • docker exec – Runs a command in a run-time container
  • docker search – Searches the Docker Hub for images
    docker search elasticsearch
    docker search mysql

    docker attach – Attaches to a running container

  • docker commit – Creates a new image from a container’s changes
  • Watch the log
docker container logs my_container
Advertisements