We have introduced the operation of Headless SSR. In this third article, we will show you how to create and use a Docker container so that you can use it easily at hand.
![](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F58J7hm5tGSQD3Iem0HgId7%2F20ee7e631a17b7f16184ce4317171249%2Fdocker12.png&w=3840&q=75)
Setting up a Docker environment
To run the container, first install Docker Desktop for Windows / macOS. Since the environment at hand is macOS, we will work with Docker Desktop for Mac.
![docker01.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F2mslpDmpWkgw3MHYsrAEzx%2F40a6e02f944a25c74e7f9ef2c7e30ab2%2Fdocker01.png&w=3840&q=75)
After startup, several dialogs will appear, but continue with the settings as they are required for authorization. After a while, the following screen will appear.
![docker02.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F7yF7SLcphjQNdZI9epGPJf%2F2e14b0e24b547c7c4c70ebc1f4d5b4e6%2Fdocker02.png&w=3840&q=75)
Run the tutorial to see how it works. You will see the command line on the right and the tutorial commands in the center.
![docker03.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F615DC8F0biOBPORIkJXvqO%2F5aa5031570683477f8443c9a95820825%2Fdocker03.png&w=3840&q=75)
Click on the command in the center, which will be copied to the command line on the right and executed. Click the Next Step button.
![docker04.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F2lsnYFdlBZtHxbN4xPSTsC%2F94fbb8127f312a94b706141aa43e67cf%2Fdocker04.png&w=3840&q=75)
A new command will appear in the center of the screen. After a few moments, the process will be finished, and when you click the Next Step button, you will be taken to the following screen
![docker05.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F4qPmC0wWaniE1ZVvB9g06y%2Fdd06ac80d70a86260f01dbfee2271377%2Fdocker05.png&w=3840&q=75)
The command shows that you want to run the container. Now, click on it to run it and access http://localhost in your browser. You will see the following site
![docker06.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F4kYzhqvAHrXZlkJV809WLe%2F452178d4512878af1ebfd71df99d2fb2%2Fdocker06.png&w=3840&q=75)
You can also click Done to see the Docker container in action.
![docker07.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2FyDRQedsT4rpla2dcyvmvP%2Ffaa9c86857b5ba26c1e20f3aa7fde749%2Fdocker07.png&w=3840&q=75)
Switching to the dashboard, you will see the following screen.
![docker08.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F6UfAK8fVeB5gN8ToAuhkJA%2F5bca691e906868a4aa41aee7ec0e2158%2Fdocker08.png&w=3840&q=75)
If you do not need it, you can delete the created image by clicking on the trash symbol shown on the far right. The environment is now ready.
Creating a Docker Image
Next, we want to create a Docker image. First, create a dockerfile in the root of the Headless SSR project folder. The code is as follows to use the Node.js 14 image.
FROM node:14
WORKDIR /usr/src/react-app-cd
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "node", "index.js" ]
Then, create a .dockerignore file to specify files that should not be imported into the image to be created.
plaintext node_modules npm-debug.log
At this time, an image is created.
powershell docker build . -t reactappcd
![docker09.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F77hGTNBWidOa9Ss9XnTZN2%2F940d92647b537af9176d04cc4fcaa5c4%2Fdocker09.png&w=3840&q=75)
Check the docker image.
![docker10.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F3rbIqlN7PVFrxnrdUo0Cki%2F21e6a5651f82d3a0a40d2e548b97bfa8%2Fdocker10.png&w=3840&q=75)
Run the container using the created image.
docker run -p 3000:3000 -d reactappcd
![docker11.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F1KxQ5NE74uYEAG76jXVuNR%2Fade37c90d5638ff947f94895546ea74e%2Fdocker11.png&w=3840&q=75)
After this, if you access http://localhost:3000 and see the page, the container is working correctly.
![docker12.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F58J7hm5tGSQD3Iem0HgId7%2F20ee7e631a17b7f16184ce4317171249%2Fdocker12.png&w=3840&q=75)
Create container execution environment
The container you have created already contains the data you have built. In order to test the new image, we will change the image to one that can directly refer to the files built in the project, since it would be time-consuming to copy and recreate the image each time.
.dockerignore changes
The files copied to dist/appname in react-app-cd should not be included in the image.
node_modules
npm-debug.log
dist
recreate an image
Create the image again in this state.
docker build . -t reactappcd
Creating docker-compose.xml
Go to the react-app project, create a new docker-compose.xml file, and write the following code.
version: '3'
services:
react-app-cd:
image: reactappcd
ports:
- 3000:3000
volumes:
- type: bind
source: ./build
target: /usr/src/react-app-cd/dist/react-app
Specify the image created in the previous step, and link the build folder to the image folder /usr/src/react-app-cd/dist/react-app.
Once the file has been created, run
docker-compose up -d
![docker13.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F2EKwHIfWnyJZqZqgvAqu4d%2Fd855f1efea74445ca4dc02f0fa473754%2Fdocker13.png&w=3840&q=75)
If the container is running, accessing http://localhost:3000 will bring up the page.
![docker12.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F58J7hm5tGSQD3Iem0HgId7%2F20ee7e631a17b7f16184ce4317171249%2Fdocker12.png&w=3840&q=75)
Summary
For the Headless SSR project, the variable parts of the project directly refer to files in the react-app project, while the other parts are created as containers. This allows us to work on the react-app project and check the operation of the built data through the container using the Headless SSR mechanism.
While the jss start command is fine for working at hand, when it is necessary to check the behavior of the actual build, the container makes it possible to test it easily.
- Headless SSR - Part.1
- Headless SSR - Part.2
- Headless SSR - Part.3