Docker Learn 1

summary of https://www.docker.com/101-tutorial/

The command you should run

1
docker run -d -p 80:80 docker/getting-started

then Open your browser to http://localhost

tips

Building the App’s Container Image

  1. Create a Dockerfile
  2. run command docker build -t getting-started . “-t” is to tag the image and “getting-started” is the name of the image. The “.” means to find Dockerfile in current directory.

Starting an App Container

  1. run docker run -dp 3000:3000 getting-started “-d” means “detached” mode. “-p” means creating a mapping between the host’s port 3000 to the container’s port 3000.

Updating our App

  1. if you have updated the source code, you need to build the updated version of the image.
    run docker build -t getting-started .
  2. And have to start a new container using the updated code.
    run docker run -dp 3000:3000 getting-started, then you will get an Error response.(WT…F)

Replacing our Old Container

  1. docker ps (find the old container ID)
  2. docker stop <the-container-id> (stop it!)
  3. docker rm <the-container-id> (delete it)
  4. you can also use docker rm -f <the-container-id> to stop and delete the container in one command.

the run docker run -dp 3000:3000 getting-started again.

Sharing our App

Create a Repo

  1. go to https://hub.docker.com/ to create one.
  2. Use the docker tag command to give the getting-started image a new name. Be sure to swap out YOUR-USER-NAME with your Docker ID.

    1
    docker tag getting-started YOUR-USER-NAME/getting-started
  3. then push it. docker push YOUR-USER-NAME/getting-started
    remember to login to Docker Desktop

  4. open https://labs.play-with-docker.com/ ,
    1
    docker run -dp 3000:3000 YOUR-USER-NAME/getting-started

Container Volumes

  1. create a volume
    1
    docker volume create todo-db
  2. stop the to-do app
    1
    2
    docker ps
    docker stop <the-container-id>
  3. start the to-do app with -v flag
    1
    docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started
    then open http://localhost:3000/ again and add some values.
  4. remove the container
    1
    2
    docker ps
    docker rm -f <container-id>
  5. Start a new container and open the app.
    1
    docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started
    the items in list should still there.

Starting a Dev-Mode Container

  1. Make sure you don’t have any of your own getting-started containers running (only the tutorial itself should be running).
  2. get in the path of ‘app’
  3. use powershell.

    1
    2
    3
    4
    docker run -dp 3000:3000 `
    -w /app -v "$(pwd):/app" `
    node:18-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 container’s present working directory where the command will run from
    • -v “$(pwd):/app” - bind mount (link) the host’s present getting-started/app directory to the container’s /app directory. Note: Docker requires absolute paths for binding mounts, so in this example we use pwd for printing the absolute path of the working directory, i.e. the app directory, instead of typing it manually
    • node:18-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.
      if you see some error message like this:
    1
    docker: Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:3000 -> 0.0.0.0:0: listen tcp 0.0.0.0:3000: bind: An attempt was made to access a socket in a way forbidden by its access permissions.

    please Open powershell with administrator rights,and restart winnat.

    1
    2
    net stop winnat
    net start winnat
  4. build your new image using docker build -t getting-started .
    you properly may see an error.

    1
    2
    3
    => CANCELED [internal] load build context
    => => transferring context: 558B
    ERROR: failed to solve: Canceled: context canceled

    you need to create a .dockerignore file in the path of app,and add node_modules/ in it.