Docker - intro

Page content

Install

macOS

  • Environment: macOS 10.15.3 (Intel chip), Mar. 2020.

Installing Docker in macOS, the official document seems to recommend installing Docker Desktop with the installer. I don’t need desktop function so far, but this is not so critical when I learn Docker. And it is the easiest way to install Docker CLI in macOS. If you don’t have DockerID, please make it first.

Related official Documents:

Don’t forget that you need to exec docker once because of permissions of macOS.

Linux

Follow official instructions.

sudo apt install ca-certificates curl gnupg lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
sudo groupadd docker
sudo usermod -aG docker $USER

Basic usage

I don’t use GUI feature. Therefore the following intro is performed on a terminal.

Download Ubuntu latest image

docker pull ubuntu:latest

Caution: Not / but : .

Running the image (container is created automatically)

docker run -it --name="UbuntuBase" ubuntu/latest

This command retrieves the imageubuntu/latest automatically and runs the container.

Notes: If you want to share a directory, using -v.

docker run -it -v /vagrant:/tmp/shared --name="myContainerName" ubuntu:latest

Exit from the container and restart

Imagine the container -> what is the difference between start, exec and run.

Exit

If you type exit inside the container which is running with -it, the container stops. If you want to attache it again, you should start the container first and attach to it. (exit) docker start UbuntuBase docker attach UbuntuBase

Detach

The meaning go option -it are

  • -i: interactive mode
  • -t: pseudo-terminal mode

https://docs.docker.com/engine/reference/commandline/attach/ If the container was run with -i and -t, you can detach from a container and leave it running using the CTRL-p CTRL-q key sequence.

docker start, exec, run

  • start: start one or more stopped containers.
  • exec: Run a command in a running container. With -it and bash command, it looks very similar to start.
  • run: Run a container from an image.

Save the image

docker commit 462f07f7b4c6 atlex00/ubuntu_updated

Stop the container

docker stop stop one or more running containers.

docker stop UbuntuBase

Notes: https://stackoverflow.com/questions/51466148/pause-vs-stop-in-docker

Upload Docker image to Docker Hub

docker push atlex00/ubuntu_updated

Tips

Run docker container with port mapping localhost:53306 to container 3306.

docker run --name mysql-test -e MYSQL_ROOT_PASSWORD=temp-root -d -p 53306:3306 mysql:8.0

Show docker containers

docker -a