Docker 简单使用流程

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

  1. 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
    
  2. open a terminal and go to the app directory with the Dockerfile.Now build the container image using the docker buildcommand

    docker 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.
  3. if you want to see the images using

    dicker image list
    

Replace the old container

  1. List the active containers and get the ID of containers

    docker ps
    
  2. Use the docker step command to stop the container

    docker stop <the-container-id>
    
  3. Once the container has stopped, you can remove it by using the docker rmcommand

    docker remove <the-cointer-id>
    
  4. Run a new container

share a repo

  1. Login to the Docker Hub using the command docker login -u YOUR-USER-NAME

  2. Use the docker tag command to give the getting-started image a new name. Be sure to swap out YOUR-USER-NAMEwith your Docker ID

    docker tag getting-started YOUR-USER-NAME/getting-started
    
  3. Try to push command

    docker push YOUR-USER-NAME/getting-started
    

Persist the DB

  1. 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"
    
  2. use the docker exec command to into the container

    docker exec <container-id> cat /data.txt
    
  3. now, let’s start another ubuntu container and we’ll see we don’t have the same file

    docker 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.

  4. 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

  1. Create a volume by using the docker volume create command

    docker volume create todo-db
    
  2. 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

  1. you can know where is docker actually storing my data by using docker volume inspectcommand
docker volume inspect todo-db

Use bind mounts

  • With bind mounts, we control the exact mountpoint on the host.
  • compare with Named volumes
Named VolumesBind Mounts
Host LocationDocker choosesYou control
Mount Example (using -v)my-volume:/usr/local/data/path/to/data:/usr/local/data
Populates new volume with container contentsYesNo
Supports Volume DriversYesNo

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!

  1. Make sure you don’t have any previous getting-started containers running.

  2. 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 using sh (alpine doesn’t have bash) and running yarn install to install all dependencies and then running yarn run dev. If we look in the package.json, we’ll see that the dev script is starting nodemon.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值