Skills for docker
docker run -d -p 80:80 docker/getting-started
-d
- Run the container in detached mode(in the background)-p
80:80 -Map port 80 of the host to port 80 in the containers.docker/getting-started
-Specify the image to use
You can combine single character flags to shorten the full command. As an example, the command above could be written as
docker run -dp 80:80 docker/getting-started
Dockerfile
-
Create a file named
Dockerfile
# syntax=docker/dockerfile:1 FROM node:12-alpine RUN apk add --no-cache python2 g++ make WORKDIR /app COPY . . RUN yarn install --production CMD ["node", "src/index.js"] EXPOSE 3000
-
open a terminal and go to the
app
directory with theDockerfile
.Now build the container image using thedocker build
commanddocker build -t getting-started .
-t
tags the image, we named the image getting-started, so we can refer to that image when we run a container..
tells Docker that is should look for the Dockerfile in the current directory.
-
if you want to see the images using
dicker image list
Replace the old container
-
List the active containers and get the ID of containers
docker ps
-
Use the
docker step
command to stop the containerdocker stop <the-container-id>
-
Once the container has stopped, you can remove it by using the
docker rm
commanddocker remove <the-cointer-id>
-
Run a new container
share a repo
-
Login to the Docker Hub using the command
docker login -u YOUR-USER-NAME
-
Use the
docker tag
command to give thegetting-started
image a new name. Be sure to swap outYOUR-USER-NAME
with your Docker IDdocker tag getting-started YOUR-USER-NAME/getting-started
-
Try to push command
docker push YOUR-USER-NAME/getting-started
Persist the DB
-
start an
ubuntu
containers that will create a file name/data.txt
with a random number between 1 and 10000.docker run -d ubuntu bash -c "shuf -i 1-10000 -n 1 -o /data.txt && tail -f /dev/null"
-
use the
docker exec
command to into the containerdocker exec <container-id> cat /data.txt
-
now, let’s start another
ubuntu
container and we’ll see we don’t have the same filedocker run -it ubuntu ls /
you will find that there’s no
data.txt
file there! That’s because of it was written to the scratch space for only the first container. -
Go ahead and remove the first container using the
docker rm -f <container-id>
command.
Container volumes
There are two main types of volumes. We will eventually use both, but we start with named volumes
-
Create a volume by using the
docker volume create
commanddocker volume create todo-db
-
Start the todo app container, but add the
-v
flag to specify a volume mount. we will use the named volume and mount int to/etc/todos
. which will capture all files create at the path.docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started
Dive into the volume
- you can know where is docker actually storing my data by using
docker volume inspect
command
docker volume inspect todo-db
Use bind mounts
- With bind mounts, we control the exact mountpoint on the host.
- compare with Named volumes
Named Volumes | Bind Mounts | |
---|---|---|
Host Location | Docker chooses | You control |
Mount Example (using -v ) | my-volume:/usr/local/data | /path/to/data:/usr/local/data |
Populates new volume with container contents | Yes | No |
Supports Volume Drivers | Yes | No |
Start a dev-mode container
- Mount our source code into the container
- Install all dependencies, including the “dev” dependencies
- Start nodemon to watch for filesystem changes
So, let’s do it!
-
Make sure you don’t have any previous
getting-started
containers running. -
Run the following command from the app directory. We’ll explain what’s going on afterwards.
If you are using an x86-64 Mac or Linux device, then use the following command.
$ docker run -dp 3000:3000 \ -w /app -v "$(pwd):/app" \ node:12-alpine \ sh -c "yarn install && yarn run dev"
-
-dp 3000:3000
- same as before. Run in detached (background) mode and create a port mapping -
-w /app
- sets the “working directory” or the current directory that the command will run from -
-v "$(pwd):/app"
- bind mount the current directory from the host in the container into the/app
directory -
node:12-alpine
- the image to use. Note that this is the base image for our app from the Dockerfile -
sh -c "yarn install && yarn run dev"
- the command. We’re starting a shell usingsh
(alpine doesn’t havebash
) and runningyarn install
to install all dependencies and then runningyarn run dev
. If we look in thepackage.json
, we’ll see that thedev
script is startingnodemon
.
-