Skip to content

Instantly share code, notes, and snippets.

@raikel
Last active May 12, 2022 14:50
Show Gist options
  • Select an option

  • Save raikel/64795ccdb6283530562d058e81228bfa to your computer and use it in GitHub Desktop.

Select an option

Save raikel/64795ccdb6283530562d058e81228bfa to your computer and use it in GitHub Desktop.
Getting started with Docker

Install Docker

sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce

Check docker daemon status with sudo systemctl status docker.

Add your user to docker group:

sudo usermod -aG docker ${USER}
su - ${USER}

Using Docker

Commands has the following structure:

docker [option] [command] [arguments]

Docker containers are build with Docker images. You can find Docker images in DockerHub.

Some commands to install with images:

docker search <search-term>  # search for a image
docker pull <image-name> # pull an image
docker images # list pulled images

Pulled images can be modified to build new ones that can then be pushed to DockerHub.

Interacting with containers:

# run a container with a given image showing an interctive console
docker run -it ubuntu

# See container logs (troubleshoot)
docker logs <container-id-or-name> 

# to exit from the container terminal
exit

# list running containers
docker ps

# list all (runing and idle) containers
docker ps -a
docker ps --format "table {{ .ID }}\t{{.Names}}\t{{.Status}}" # format outout

# start a previously stopped container
docker start <container-id-or-name>

# stop a running container
docker stop <container-id-or-name> 

# get a terminal into the container
docker exec -it <container-id-or-name> bash

# get a terminal into the container as root
docker exec -u root -it <container-id-or-name> bash

# remove a container
docker rm <container-id-or-name>

# Commit changes made to an image
docker commit -m "<description>" -a "<your-name>" <container_id> <repository>/<image_name>

# list images
docker images

# remove image
docker rmi <image-id-or-name>

# remove all images
docker rmi $(docker images -q)

# Copy file from container to host
docker cp <containerId>:/file/path/within/container /host/path/target

# See the IP address of a container to access its network from the host
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_id>

References

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04-es https://docs.docker.com/engine/install/ubuntu/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment