Docker learn
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
- Create a Dockerfile
- 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
- 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
- if you have updated the source code, you need to build the updated version of the image.
rundocker build -t getting-started .
- And have to start a new container using the updated code.
rundocker run -dp 3000:3000 getting-started
, then you will get an Error response.(WT…F)
Replacing our Old Container
docker ps
(find the old container ID)docker stop <the-container-id>
(stop it!)docker rm <the-container-id>
(delete it)- 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
- go to https://hub.docker.com/ to create one.
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
then push it.
docker push YOUR-USER-NAME/getting-started
remember to login to Docker Desktop- open https://labs.play-with-docker.com/ ,
1
docker run -dp 3000:3000 YOUR-USER-NAME/getting-started
Container Volumes
- create a volume
1
docker volume create todo-db
- stop the to-do app
1
2docker ps
docker stop <the-container-id> - start the to-do app with
-v
flagthen open http://localhost:3000/ again and add some values.1
docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started
- remove the container
1
2docker ps
docker rm -f <container-id> - Start a new container and open the app.the items in list should still there.
1
docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started
Starting a Dev-Mode Container
- Make sure you don’t have any of your own
getting-started
containers running (only the tutorial itself should be running). - get in the path of ‘app’
use powershell.
1
2
3
4docker 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
2net stop winnat
net start winnatbuild 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 canceledyou need to create a
.dockerignore
file in the path ofapp
,and addnode_modules/
in it.
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.
Comments