Docking your Images

 So after a few months of work and experimentation, I have a few apps lying around that should be archived. I began the process of turning them in to repositories for GitHub, but then it hit me that a few of these apps are going to be a pain to recreate their development environment should the unspeakable happen and I had to start from scratch with a new pc. It's not as simple as installing Xampp and VS Code.

Should I want to work on them later, I want to just download and start coding. Thus, the solution was converting my app into an image with which I could run as a container at a later date. It had been a while since I used Docker. Deploying a Node app didn't count, as GitHub Actions basically did all the work pushing the image to Docker Hub. So this was the first time I was using Docker. 

Time to open that container

Well, first thing to note is that Docker is definitely not Git. It's easy to forget that Git and GitHub are used for version control when most of the time I use them as a cloud save. Sure, Docker Hub is the same, but images are a lot more than source code. Images are self-contained virtual environments with the intent of preserving the development environment of an application. In short, once an image is made, anyone can run this as a container and the experience will be the same.

So the first step is to create a Dockerfile at the root of the application. This dockerfile lays out the plan for our image. Dockerfiles can be real complex when it comes to certain projects, but for this one I only needed a few fields. 

FROM node:17 
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
EXPOSE 5000
CMD ["npm","start"]

FROM defines the development environment. It could be PHP, Python or HTML5 FROM list the platform and the version used. This app uses NodeJS v17. WORKDIR tell docker where the image files will be stored. In this case, we are storing them in an app folder. COPY is a straight forward as it copies files from the app to the image folder. EXPOSE makes an open app port available outside the container. This app uses port 5000. Finally, CMD will execute a command once the container has been completed.
Of course, RUN is different, as it executes commands during build of the image.  Missing is VOLUME which allow a section of the container to be accessible from the outside and ENV sets the container's environment variables.

With the Dockerfile completed, the next step was to build the image file. Inside of command prompt, I CD to the app directory and entered docker build -t appname:version .  which built the image using the app name and version. The "." represented the location which was the current directory. This process took a few minutes and when it was over I used docker run -it -p 3000:5000 appname:version to test that my image was working. -p 3000:5000 mapped the internal port 5000 to an external port 3000. Thus, from my browser, I was able to localhost:3000 into my app. With that confirmed, all that was left was to create a new repository and upload my image.  The new repository was username/reponame  and thus I had to change the tag on my image before I could push the image. In command prompt, I entered docker tag appname:version username/reponame. Then finally docker push username/reponame and my image was added to my repository.  

 All it all it wasnt as hard as I thought it would have been and white I did encounter some issues for the most part I can easily replicate this process easily. 

 


 


Post a Comment

Previous Post Next Post