I wanted to practice Create React App a little detour without polluting the local environment. Therefore, we aimed to build an environment like the title.
Final directory structure
node-docker/ |--docker-compose.yml |--node/ |--app/
Docker Compose Overview Compose is a tool for Docker applications that define and run multiple containers.
First, create the project folder
node-docker and create the
docker compose.yml file there.
Current directory structure
version: '3' services: node: image: node:14.9.0-alpine3.10 container_name: node volumes: - ./node/app:/app tty: true ports: - 3000:3000
File format version of
You can define each container as a service.
Specifies the original image when running the container.
If the image doesn't exist, Compose will try to pull it from Docker Hub.
To create an image from
Dockerfile, for example, create
Dockerfile under the
node directory and specify as follows.
Specify a custom container name instead of the default generated name. By default, it will be named node-docker_node_1.
Share the local path (left) and the container path (right).
Specifies whether to start the terminal. (Maybe)
Publish the port. Specify the host and port (host: container), or specify only the port of the container (the port on the host side is randomly selected).
expose exposes the port and is only accessible between linked services.
Now that you have the necessary files, let's create and start the container.
If you want to create an image from
Dockerfile, you need to do
Docker-compose build, but this time you don't need it because you will get the already built image remotely.
Work is done on the directory where
docker-compose.yml is located.
$ docker-compose up -d
d option allows it to start in the background.
Now that the node environment is in place, the node environment is on the container, so you need to work on it.
For that purpose, start the shell and terminal on the container by using the
exec command that can execute the specified command in the running container.
$ docker-compose exec node sh
The directory structure in the container is as follows, and since it was specified in
app directory in the container and the local
node / app directory correspond.
Directory structure in the container
/ |--app |--bin |--dev |--home |--lib |--media |--mnt |--opt |--proc |--root |--run |--sbin |--srv |--sys |--tmp |--usr |--var
I think it's better to work in the app directory that can be shared locally.
Compose file version 3 reference Compose File Reference Docker Compose --docker-compose.yml reference [For beginners] Easy Node.js development environment construction with Docker (2)