loading...
Cover image for Simplifying docker execution on DEV time!

Simplifying docker execution on DEV time!

joelbonetr profile image JoelBonetR Updated on ・3 min read

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 docker-compose up command to raise your docker container
3- Run some docker command docker ps to see which name the container has
4- Run another docker command to execute a specific docker image (previously raised on step 2) docker exec -it docker-image-name
5- Set terminal context inside docker image (adding /bin/bash to docker exec command docker exec -t -i container_name /bin/bash 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:
parcel index.html or parcel index.js
Or using npm, npx, yarn...
yarn start (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 SO, you'll need to adapt it -

#!/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";     

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 ./docker-run.sh 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

Bonus

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).

fuser -k 1234/tcp 

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.

JoelBonetR

Posted on May 5 by:

joelbonetr profile

JoelBonetR

@joelbonetr

Senior Web Developer- Intermediate Grade on Computer Systems- High Grade on Web Application Development- Coursed a non-official MBA with Marketing and HR

Discussion

markdown guide