Docker
Check whether docker is running:
- Find the
PIDof theDockerdaemon:
ps -ef | grep dockerIf the output contains a line with /usr/local/bin/dockerd (or a similar path) and shows a numeric PID, the Docker daemon is running.
- Use
docker ps
docker ps is mainly for listing running containers, but if the Docker service isn't running it returns an error. So you can also run it and watch the output to tell whether Docker is up.
If the service is running, it lists all running containers (or just the header row when none are running). If the service is down, you'll see an error such as Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
- Use
docker info
docker info prints detailed information about the Docker system — the number of containers and images, the Docker version, and more. If the service is running, this command outputs normally; otherwise you'll see an error similar to docker ps.
Install Docker from: https://hub.docker.com/
Images
Docker packages an application and its dependencies into an image file. Only through this file can a Docker container be created — an image is essentially a template for containers. Docker creates container instances from an image, and the same image can spawn multiple containers running at the same time.
An image is a binary file. In practice, an image is usually built by inheriting from another image and adding some customizations.
# Search for an image
$ docker search [keywords]
# List all local image files
$ docker image ls
# Remove an image file
$ docker image rm [imageName]
$ docker image rmi [imageID]Pull an image from a registry
- Pull an
imagefrom a registry to the local machine:
$ docker image pull library/hello-world
# docker image pull fetches an image file. library/hello-world is the image location in the registry:
# library is the group the image belongs to, and hello-world is the image name.
# Official images all live under library, which is the default group and can be omitted:
# $ docker image pull hello-world- After a successful pull, the
imagefile is visible locally:
$ docker image ls- Run / create a container from the
image:
$ docker container run hello-world
# docker container run creates a running container from an image file.
# If the image isn't present locally it is fetched automatically, so docker image pull isn't strictly required.docker container runcreates a new container every time it runs. To reuse an existing (created or stopped) container:
# Start an already-created or stopped container
$ docker container start [containerID]- View a container's output:
# Show the output of a docker container
$ docker container logs [containerID]
# Use this when docker run was started without the -it flag- Enter a running container:
# When docker run was started without -it, use this to get a shell inside the container
$ docker container exec -it [containerID] /bin/bash- Stop a container:
# Kill the container immediately
$ docker container kill [containerID]
# Stop the container gracefully (may run cleanup, so it doesn't always stop instantly)
$ docker container stop [containerID]Container
- A container instance created from an
imageis itself a file, called a container file. - Once a container is created, two files exist together: the
imagefile and the container file. Stopping a container does not delete the container file — it only stops running.
# List running containers on this machine
$ docker container ls
# List all containers, including stopped ones
$ docker container ls --all- Stopped container files still take up disk space. You can delete them; after deletion they no longer appear in the list.
$ docker container rm [containerID]Create a container
docker container runcreates a container from animagefile:
$ docker container run -p 8000:3000 -it koa-demo /bin/bash
# or
$ docker container run -p 8000:3000 -it koa-demo:0.0.1 /bin/bash
# -p: map the container's port 3000 to the host's port 8000
# -it: connect the container's shell to the current shell, so commands you type are passed into the container
# koa-demo:0.0.1: the image name (add the tag if there is one; the default tag is latest)
# /bin/bash: the first command run inside the container after it starts — here it launches Bash so you get a shell- Now you're inside the container and can run commands:
root@66d80f4aaf1e:/app# node demos/01.js- Note that the node process runs inside Docker's virtual environment; the file system and network interfaces it sees are virtual and isolated from the host, which is why you need to define port mappings between the container and the host.
- Stop the container and free memory:
# In the container shell, press Ctrl + C to stop the node process, then Ctrl + D (or type exit) to leave the container
# Or use docker container kill to terminate it
# You may need another host terminal to look up the container ID
$ docker container ls
# or
$ docker container ls --all
# Stop the container
$ docker container kill [containerID]
# A stopped container doesn't disappear; you need to delete the container file
# Delete a specific container file
$ docker container rm [containerID]
# or
# Use --rm to auto-delete the container file once it stops
$ docker container run --rm -p 8000:3000 -it koa-demo /bin/bashDockerfile
- A text file used to configure an
image.Dockerbuilds the binaryimagefile from it. - First grab a project:
$ git clone https://github.com/ruanyf/koa-demos.git
$ cd koa-demos- Inside the project, write a
.dockerignorefile. The three paths below should be excluded and not packaged into theimage:
.git
node_modules
npm-debug.log- Create a
Dockerfilein the root:
FROM node:8.4
# This image inherits the official node image; the colon marks the tag — node version 8.4 here
COPY . /app
# Copy everything in the current directory (except .dockerignore exclusions) into /app in the image
WORKDIR /app
# Set the working directory for the following steps to /app
RUN npm install
# Run npm install under /app. All installed dependencies get baked into the image
EXPOSE 3000
# Expose the container's port 3000 so external connections are allowed- With a
Dockerfilein place, build theimagewithdocker image build:
$ docker image build -t koa-demo .
# -t sets the image name; the trailing . is the path to the Dockerfile (the current directory here)
# or
$ docker image build -t koa-demo:0.0.1 .
# You can append a tag with a colon. If omitted, the default tag is latest.
# Once it succeeds, check with docker image lsCMD
- A command that runs when the container starts, such as the
node demos/01.jswe ran manually above. You can put it in theDockerfile:
FROM node:8.4
COPY . /app
WORKDIR /app
RUN npm install --registry=https://registry.npmmirror.com
EXPOSE 3000
# Runs automatically after the container starts
CMD node demos/01.js- Difference between
CMDandRUN:RUNexecutes during the image build; its results are baked into the image fileCMDexecutes after the container starts- A Dockerfile can have multiple
RUNcommands but only oneCMD - Once
CMDis set,docker container runcan't append a command (such as the earlier/bin/bash), otherwise it overridesCMD:
$ docker container run --rm -p 8000:3000 -it koa-demo:0.0.1Troubleshooting
- If Docker Desktop can't sign in, try
docker login. Signing in from the command line prints a clearer error; if it's a network problem, switch to a different proxy. Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
# Linux: the docker service isn't started
$ sudo service docker start
# or, on distros using systemd:
$ sudo systemctl start dockerOn macOS or Windows with Docker Desktop, there's no service/systemctl binary — start or restart the Docker Desktop application instead; the daemon runs inside its VM, not as a host-level service.
References
- Docker getting-started tutorial
- Understanding Docker from zero
- Docker official site
- Docker application deployment
- No vim inside Docker
- Configuring a domestic apt-get source
- Front-end engineer Docker tutorial — hands-on
- Docker won't stop after starting
- Docker advanced series: docker-compose / swarm / stack / secret / config
- docker-compose getting-started guide
- Deleting a Docker image