Simplifying docker execution on DEV time!
Tags: docker, productivity, beginners, tutorial
Published: 2020-05-05 09:21:53

---
title: Simplifying docker execution on DEV time!
published: true
description: How to brainless serve your dockerized projects
tags: docker, productivity, beginners, tutorial
cover_image: https://hoplasoftware.com/wp-content/uploads/2017/10/Dise%C3%B1o-sin-t%C3%ADtulo-62.png
---
<meta name="monetization" content="$ilp.uphold.com/7QaxJkDwRZED">

If you use docker you must find yourself on a cyclic yet repetitive workaround which look kinda similar to this:

1- Open a terminal on your project's root dir.
2- Run a <code>docker-compose up</code> command to raise your docker container
3- Run some docker command <code>docker ps</code> to see which name the container has
4- Run another docker command to execute a specific docker image (previously raised on step 2) <code>docker exec -it docker-image-name</code>
5- Set terminal context inside docker image (adding /bin/bash to docker exec command <code>docker exec -t -i container_name /bin/bash</code> or using SSH method or whatever you are used to.
6- Launch a watch / serve command against some bundler (or maybe some other command if you're with a different stack as mine). 
Basic example with parcel:
<code>parcel index.html</code> or <code>parcel index.js</code>
Or using npm, npx, yarn...
<code>yarn start</code> (or yarn [command], beign command the name you set into package.json file)


We started using docker time ago trying different approaches, long story short, after a week using it, i was bored on typing the command once, twice or more a day so I'll coded a simple script to wrap it all - I'm working on Linux (deb based), if you use another OS, you'll need to adapt it -

<pre>
#!/bin/bash
docker-compose up &
sleep 3 &&
echo "context inside docker" &&
docker exec -it docker-image-name /bin/bash -c "cd /var/www/html/_dev/; parcel index.html";     

</pre>

This will perform following actions;
- raise the docker container (docker-compose up)
- wait 3 seconds (it's a fast process but you need it to complete before trying next step, otherwise you'll get an error as you can't exec things inside a non-raised container).
- exec the container, with terminal context on it, cd into projects root dir.
- launch the watch / serve / build command and show the output on the same terminal.

You can now close easily the watch/serve process and shutdown the docker container simply pressing ctrl + C on that same terminal.

You must edit docker image name and route to navigate before serve the project, also project serve command (or put on it the command you need), and save it as "docker-run.sh" on your project's root dir (with execution permission for non-root user). 

Then, all you have to do early in the morning is open your project dir and launch <code>./docker-run.sh </code> and have a drink of coffee while the script does the job for you.

Isn't IT intended to automate processes for more (and brainless) productivity? :D

<h2>Bonus</h2>
If you use a bundler that serves your project as localhost:somePort, you may find yourself with a locked port (sometimes when typing something incorrect or hitting the wrong key with the watch/serve running, it may cause failure, sometimes not releasing the port and then you can't run the serve/watch again instead you release it first manually.
Here's the command (depending on the distro may require you to install something to work).

<pre>fuser -k 1234/tcp </pre>

If you use different ports for each project, you also can save the command with the specific port inside root dir of a project as .sh to automate this job.


Hope it helps you,

best regards.


<a href="https://joelbonetr.com/">JoelBonetR</a>